const http = require('http')
const server = http.createServer();
//这个模块能够帮我们解析url地址 从而拿到pathname query
const urlModule = require('url')

server.on('request',(req,res)=> {
  //write your code for here
  // const url = req.url
  const {pathname:url, query} = urlModule.parse(req.url,true)

  if (url === 'getscript') {

    var data = {
      name: 'xjj',
      age: 18,
      gender: 'female'
    }

    //拼接一个合法的js脚本，这里拼接的是一个方法的调用
    var scriptStr = `${query.callback}(${JSON.stringify(data)})`
    //res.end 发送给 客户端，客户端去把这个字符串，当作js代码去解析执行
    res.end(scriptStr);
  }else {
    res.end(400);

  }
})

server.listen(3000, () => {
  console.log('server listen at http://127.0.0.1:3000')
})